package com.free.handle;

import com.alibaba.fastjson.JSON;
import com.free.common.entity.CustomProtocol;
import com.free.common.entity.ReqInfoDto;
import com.free.common.utils.SpringUtil;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author hao.ding@insentek.com
 * @date 2021-10-29 10:18
 */
@Slf4j
public class EchoClientHandle extends SimpleChannelInboundHandler<ByteBuf> {

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {

        if (evt instanceof IdleStateEvent){
            IdleStateEvent idleStateEvent = (IdleStateEvent) evt ;

            if (idleStateEvent.state() == IdleState.WRITER_IDLE){
                log.info("已经 10 秒没有发送信息！");
                //向服务端发送消息
                ReqInfoDto heartBeat = SpringUtil.getBean("heartBeat", ReqInfoDto.class);
                log.info(JSON.toJSONString(heartBeat));
                CustomProtocol customProtocol = new CustomProtocol();
                customProtocol.setId(1);
                customProtocol.setContent(JSON.toJSONString(heartBeat));
                ctx.writeAndFlush(heartBeat).addListener(ChannelFutureListener.CLOSE_ON_FAILURE) ;
            }
        }

        super.userEventTriggered(ctx, evt);
    }

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf in) throws Exception {

        //从服务端收到消息时被调用
        log.info("客户端收到消息={}",in.toString(CharsetUtil.UTF_8)) ;

    }
}
